package org.example.util;
import org.example.processor.FeatureStorage;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.ops.transforms.Transforms;

import java.util.Map;

public class SimilaritySearch {
    public static String findMostSimilar(INDArray queryFeature) {
        String resurt = null;
        String mostSimilarId = null;
        double maxSimilarity = -1;

        for (Map.Entry<String, INDArray> entry : FeatureStorage.featuresMap.entrySet()) {
            double similarity = Transforms.cosineSim(queryFeature, entry.getValue());
            if (similarity > maxSimilarity) {
                maxSimilarity = similarity;
                mostSimilarId = entry.getKey();
                System.out.println("mostSimilarId"+"的相似度为："+similarity+"相似度为："+mostSimilarId);
                resurt = "图片和 ："+mostSimilarId+"最相似，相似度为："+maxSimilarity;
            }
        }
        return resurt;
    }
}

